import db from '../database/db.js'
import bcrypt from 'bcryptjs'

export default class User {

	static async register(req, res) {
		const { username, password, nickname, repwd } = req.body;
		// 用户是否存在
		const isExists = await User.findByUsername(username)
		if (isExists) return res.cc('用户已存在！')
		
		// 两次密码是否输入一致
		if(password !== repwd) return res.cc('两次密码输入一致！')
		
		// 添加注册用户
		const hashedPassword = bcrypt.hashSync(password, 10)
		const [result] = await db.execute(
		    'INSERT INTO users (username, password, nickname) VALUES (?, ?, ?)',
		    [username, hashedPassword, nickname]
		);
		
		
		
			
	}

	static async login(req, res) {

	}

	static async findByUsername(username) {
		const [rows, fields] = await db.execute(
		'SELECT * FROM users WHERE username = ?',
		 [username],
		 )
		return rows[0]
	}
}